package yuejian_07.yuejian_07.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import yuejian_07.yuejian_07.R;
import yuejian_07.yuejian_07.activity.ShowFabuActivity;
import yuejian_07.yuejian_07.adapter.ShowListAdapter;
import yuejian_07.yuejian_07.javabean.Show;
import yuejian_07.yuejian_07.model.ShowModel;


public class ShowFragment extends Fragment {

    private RecyclerView recyclerView;

    private View view=null;
    private Button fabu;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        view=inflater.inflate(R.layout.tab04_show, container, false);

        initViews();
        initEvents();
        initData();
        return view;

    }
    private void initViews(){

        recyclerView= (RecyclerView)view.findViewById(R.id.rv_show);
        recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        fabu=(Button) view.findViewById(R.id.ic_fabu);

    }

    private void  initEvents(){
        fabu.setOnClickListener(new View.OnClickListener(){//购买跳转
            @Override
            public void onClick(View v){
                Intent intent = new Intent(ShowFragment.this.getActivity(), ShowFabuActivity.class);
                startActivity(intent);
            }
        });

    }

    private void initData()
    {

        ShowModel showModel = new ShowModel();
        Call call = showModel.showlist();
        call.enqueue(new Callback<List<Show>>() {
            @Override
            public void onResponse(Call<List<Show>> call, Response<List<Show>> response) {
                List<Show>mDataList=  response.body();

                recyclerView.setAdapter(new ShowListAdapter(getActivity(),mDataList));
            }

            @Override
            public void onFailure(Call<List<Show>> call, Throwable t) {
                Toast.makeText(getActivity(), "fail", Toast.LENGTH_SHORT).show();
            }
        });

    }
}
